package com.bustle.web.mbean.request;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.bustle.ejb.entity.User;
import com.bustle.ejb.service.AuthenticationManager;
import com.bustle.ejb.service.dao.AddressDao;
import com.bustle.web.Report;
import com.bustle.web.mbean.session.UserMBean;

@ManagedBean
@SessionScoped
public class MonCompteMBean {
	
	private User user;
	private boolean displayView = true;
	private boolean displayUpdate = false;
	@ManagedProperty("#{userMBean}")
	private UserMBean userMBean;

	@EJB
	private AuthenticationManager authenticationManager;
	
    @EJB
    private AddressDao addressDao;
	
    protected String firstName,lastName,email,city,department,name_street,postcode;
	protected Date birthday;

	

	@PostConstruct
	public void postBuild(){
		user=authenticationManager.getUser(userMBean.getId());
		firstName=user.getFirstName();
		lastName=user.getLastName();
		birthday=user.getBirthday();
		email=user.getEmail();
		name_street=user.getAddress().getName_street();
		postcode=user.getAddress().getPostcode();
		city=user.getAddress().getCity();
		department=user.getAddress().getDepartment();
	}
	

	public String save() {
		System.out.println("save");
		try {
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setBirthday(birthday);
			user.setEmail(email);
			user.getAddress().setName_street(name_street);
			user.getAddress().setPostcode(postcode);
			user.getAddress().setCity(city);
			user.getAddress().setDepartment(department);
			authenticationManager.updateUser(user);
			addressDao.update(user.getAddress());
			displayView = true;
			displayUpdate = false;
			return null;
		} catch (Exception anException) {
			Report.error("Unable to save " + "firstName" + " " + "lastName",
					anException);
		}
		
		return null;
	}
	
	public String modify() {
		displayView = false;
		displayUpdate = true;
		return null;
	}
	
	
	public AuthenticationManager getAuthenticationManager() {
		return authenticationManager;
	}
	
	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
		this.authenticationManager = authenticationManager;
	}
	
	public AddressDao getAddressDao() {
		return addressDao;
	}

	public void setAddressDao(AddressDao addressDao) {
		this.addressDao = addressDao;
	}

	public String getFirstName() {
		return firstName;
	}



	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}



	public String getLastName() {
		return lastName;
	}



	public void setLastName(String lastName) {
		this.lastName = lastName;
	}



	public String getEmail() {
		return email;
	}



	public void setEmail(String email) {
		this.email = email;
	}



	public String getCity() {
		return city;
	}



	public void setCity(String city) {
		this.city = city;
	}



	public String getDepartment() {
		return department;
	}



	public void setDepartment(String department) {
		this.department = department;
	}



	public String getName_street() {
		return name_street;
	}



	public void setName_street(String name_street) {
		this.name_street = name_street;
	}



	public String getPostcode() {
		return postcode;
	}



	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}



	public Date getBirthday() {
		return birthday;
	}



	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}




	
	

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public boolean isDisplayView() {
		return displayView;
	}


	public void setDisplayView(boolean displayView) {
		this.displayView = displayView;
	}


	public boolean isDisplayUpdate() {
		return displayUpdate;
	}


	public void setDisplayUpdate(boolean displayUpdate) {
		this.displayUpdate = displayUpdate;
	}


	public UserMBean getUserMBean() {
		return userMBean;
	}

	public void setUserMBean(UserMBean userMBean) {
		this.userMBean = userMBean;
	}


}

